import React, {Component} from 'react'
import { Map, is } from 'immutable';
import {
    Button,
    Input,
    Select,
    Form,
    Row,
    Col,
    Radio,
    message,
    DatePicker,
} from 'antd';
import { SelectUrl } from '../../../components';
import { fetchData } from '../../../components/AjaxConsumer'
const FormItem = Form.Item;
const Option = Select.Option;
const RadioGroup = Radio.Group;
import moment from 'moment';
const dateFormat = 'YYYY-MM-DD HH:mm:ss'; //DatePicker格式
const formItemLayout = {
    labelCol: {
        span: 6
    },
    wrapperCol: {
        span: 14
    },
};
const buttonItemLayout  = {
    wrapperCol: {
        span: 24,
        offset: 9,
    },
};

export default class StatusRuleForm extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            isAdd: true, //办单默认是新增一条数据
            objectInfo: {}
        };
    }

    //提交事件
    saveHandle = (values) => {
        const options= {
            url: '/base/productstatusrule/u',
            type: 'post',
            condition:values
        };
        fetchData(this, options, (resData) => {
            if(resData.code == "1"){
                message.success('保存成功!');
                this.resetHandle();//添加成功后重置表单
                this.props.refreshHandle();//添加成功后刷新list列表
            }else{
                message.error("请求数据错误：" + resData.message);
            }
        });
    }

    addHandle = (values) => {
        let searchCondition = this.props.searchCondition;//查询条件，创建新数据拼装上查询条件
        let addData = {...values, ...searchCondition}

        console.log("addData====", addData)

        const options= {
            url: '/base/productstatusrule/c',
            type: 'post',
            condition:addData
        };
        fetchData(this, options, (resData) => {
            if(resData.code == "1"){
                message.success('新增成功!');
                this.resetHandle();//添加成功后重置表单
                this.props.refreshHandle();//添加成功后刷新list列表
            }else{
                message.error("请求数据错误：" + resData.message);
            }
        });
    }

    deleteHandle = () => {

        //TODO 执行删除事件

    }

    resetHandle = () => {
        this.props.form.resetFields();
    }
    handleSubmit = (e) => {
        e.preventDefault();
        this.props.form.validateFieldsAndScroll((err, fieldsValue) => {
            if (!err) {
                const values = {//有时间类型的需要再次处理
                    ...fieldsValue
                    //,
                    //'gmtCreateStr': fieldsValue['gmtCreateStr'].format('YYYY-MM-DD HH:mm:ss')
                }
                console.log('form表单值=====', values);
                if(this.state.isAdd){//新增添加
                    this.addHandle(values);
                }else{//修改保存
                    this.saveHandle(values);
                }
            }
        });
    }

    componentWillReceiveProps(nextProps) {
        let blCompare = false;  //比较搜索条件是否变化，如果没有变化不执行查询
        const map1 = Map(this.props.rowObject);
        const map2 = Map(nextProps.rowObject);
        if(!is(map1, map2)){
            blCompare = true;
        }
        if(blCompare){
            this.props.form.resetFields();
            this.setState({
                isAdd: nextProps.isAdd,
                objectInfo:  nextProps.rowObject
            });
        }
    }

    render() {
        const { getFieldDecorator, setFieldsValue } = this.props.form;
        let objectInfo = this.state.objectInfo;

        let actionButtons;
        if(this.state.isAdd){//新增
            actionButtons = (

                <Row className="infoForm_row">
                    <FormItem {...buttonItemLayout}>
                        <Col span={2} >
                            <Button type='primary' htmlType="submit" >新增</Button>
                        </Col>
                        <Col span={2} >
                            <Button type='default' onClick={this.resetHandle}>重置</Button>
                        </Col>
                    </FormItem>
                </Row>
            )
        }else{//修改、删除
            actionButtons = (
                <Row className="infoForm_row">
                    <FormItem {...buttonItemLayout}>
                        <Col span={2} >
                            <Button type='primary' htmlType="submit">保存</Button>
                        </Col>
                        <Col span={2} >
                            <Button type='danger' onClick={this.deleteHandle}>删除</Button>
                        </Col>
                        <Col span={2} >
                            <Button type='default' onClick={this.resetHandle}>重置</Button>
                        </Col>
                    </FormItem>
                </Row>
            )
        }
        return (
            <div className="infoForm">
                <Form onSubmit={this.handleSubmit}>
                    <FormItem  style={{display: 'none'}}>
                        {
                            getFieldDecorator('proStatusRuleId', {
                                rules: [{ required: false}],
                                initialValue: objectInfo.proStatusRuleId? objectInfo.proStatusRuleId : ""
                            })(
                                <Input placeholder="请输入" />
                            )}
                    </FormItem>

                    <Row className="infoForm_row">
                        <Col span="8" >
                            <FormItem  {...formItemLayout} label="产品代码">
                                {
                                    getFieldDecorator('productCode', {
                                        //rules: [{ required: true, message: '请输入产品代码', whitespace: true }],
                                        initialValue: this.props.searchCondition.productCode? this.props.searchCondition.productCode : ""
                                    })(
                                        <Input disabled="true" />
                                    )}
                            </FormItem>
                        </Col>
                        <Col span="8" >
                            <FormItem  {...formItemLayout} label="仓库编码">
                                {
                                    getFieldDecorator('whCode', {
                                        //rules: [{ required: true, message: '请输入仓库编码', whitespace: true }],
                                        initialValue: objectInfo.whCode? objectInfo.whCode : ""
                                    })(
                                        <Input disabled="true" />
                                    )}
                            </FormItem>
                        </Col>
                        <Col span="8" >
                            <FormItem  {...formItemLayout} label="客户编码">
                                {
                                    getFieldDecorator('custCode', {
                                        //rules: [{ required: true, message: '请输入客户编码', whitespace: true }],
                                        initialValue: objectInfo.custCode? objectInfo.custCode : ""
                                    })(
                                        <Input placeholder="请输入"/>
                                    )}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row className="infoForm_row">
                        <Col span="8" >
                            <FormItem  {...formItemLayout} label="产品状态">
                                {
                                    getFieldDecorator('productStatus', {
                                        //rules: [{ required: true, message: '请输入产品状态', whitespace: true }],
                                        initialValue: objectInfo.productStatus? objectInfo.productStatus : ""
                                    })(
                                        //<Input placeholder="请输入" />
                                        <SelectUrl url='/base/dic/get/product_status' dicCode="dicCode" dicName="dicName" />
                                    )}
                            </FormItem>
                        </Col>
                        <Col span="8" >
                            <FormItem  {...formItemLayout} label="是否序列号采集">
                                {
                                    getFieldDecorator('serialCollect', {
                                        //rules: [{ required: true, message: '请输入是否序列号采集', whitespace: true }],
                                        initialValue: objectInfo.serialCollect? objectInfo.serialCollect : ""
                                    })(
                                        //<Input placeholder="请输入" />
                                        <SelectUrl url='/base/dic/get/serial_collect_flag' dicCode="dicCode" dicName="dicName" />
                                    )}
                            </FormItem>
                        </Col>
                        <Col span="8" >
                            <FormItem  {...formItemLayout} label="入库序列号采集">
                                {
                                    getFieldDecorator('instockSerial', {
                                        //rules: [{ required: true, message: '请输入入库序列号采集', whitespace: true }],
                                        initialValue: objectInfo.instockSerial? objectInfo.instockSerial : ""
                                    })(
                                        <Input placeholder="请输入" />
                                    )}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row className="infoForm_row">
                        <Col span="8" >
                            <FormItem  {...formItemLayout} label="出库序列号采集">
                                {
                                    getFieldDecorator('outstockSerial', {
                                        //rules: [{ required: true, message: '请输入出库序列号采集', whitespace: true }],
                                        initialValue: objectInfo.outstockSerial? objectInfo.outstockSerial : ""
                                    })(
                                        <Input placeholder="请输入" />
                                    )}
                            </FormItem>
                        </Col>
                    </Row>
                    {/*操作按钮*/}
                    {actionButtons}
                </Form>
            </div>
        );
    }
}
